﻿using Microsoft.Graphics.Canvas;
using System;
using System.Collections.Generic;

using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.PageViews
{
    [PageType("cpu","漫画化")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class Cartoon : Page
    {

        int width = 0;
        int height = 0;

        Color[] colors;
        Color[] tempColors;
        WriteableBitmap wb;

        byte[] co;

        public Cartoon()
        {
            this.InitializeComponent();
        }

        private async void selectPicture_Click(object sender, RoutedEventArgs e)
        {
            CanvasBitmap bitmap = await Function.SelectImageAsync();
            if (bitmap == null)
            {
                return;
            }
            Function.InitialParameter(bitmap, out width, out height, out colors, out tempColors, image, out wb);

            co = new byte[width * 4 * height];
            bitmap.GetPixelBytes().CopyTo(co, 0);

        }


        /// <summary>
        /// 漫画化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cartoon_Click(object sender, RoutedEventArgs e)
        {
            //模糊效果
            var gaussian = PictureFilter.Win2dGaussianBlurEffect(colors, width, height, (float)gaussin.Value);
            //油画效果
            var oilpaintColors = PictureFilter.OilpaintPaintEffect(gaussian, width, height, (int)r.Value, (int)number.Value);
            //画笔效果
            var sobelColors = PictureFilter.PencilEffect(oilpaintColors, width, height, (int)white.Value);
            Color[] cartoonColors = new Color[colors.Length];
            //正片叠底效果
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int index = x + y * width;
                    var oilpanint = oilpaintColors[index];
                    var sobel = sobelColors[index];
                    byte r = (byte)((oilpanint.R * sobel.R) / 255);
                    byte g = (byte)((oilpanint.G * sobel.G) / 255);
                    byte b = (byte)((oilpanint.B * sobel.B) / 255);
                    Color cartoon = Color.FromArgb(255, r, g, b);
                    cartoonColors[index] = cartoon;
                }

            }

            Function.InitialImage(cartoonColors, width, height, image1, wb);

        }

        /// <summary>
        /// 油画
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void painting_Click(object sender, RoutedEventArgs e)
        {
            var tempValue = PictureFilter.OilpaintPaintEffect(colors, width, height, 2, 4);
            Function.InitialImage(tempValue, width, height, image1, wb);
        }

        private void painting3_Click(object sender, RoutedEventArgs e)
        {
            var tempValue = PictureFilter.OilpaintPaintEffect3(colors, width, height, (int)r.Value, (int)number.Value);
            Function.InitialImage(tempValue, width, height, image1, wb);
        }
    }
}
